//
//  TouchIDVC.m
//  Xspcf
//
//  Created by 小小算盘 on 16/8/17.
//  Copyright © 2016年 小小算盘. All rights reserved.
//

#import "TouchIDVC.h"
#import <LocalAuthentication/LocalAuthentication.h>
#import "SunLoginCheckVC.h"
@interface TouchIDVC (){
    NSTimer *_timer;
    BOOL _canLogin;
}
@end

@implementation TouchIDVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    UIImageView *img = [[UIImageView alloc]init];
    [self.view addSubview:img];
    [img mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(0));
        make.top.equalTo(@(0));
        make.bottom.equalTo(@(0));
        make.right.equalTo(@(0));
    }];
    img.image = [UIImage imageNamed:@"指纹登录"];
    img.userInteractionEnabled = YES;
    UIButton *btn = [[UIButton alloc]init];
    [img addSubview:btn];
    [btn setImage:[UIImage imageNamed:@"登录指纹"] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(login) forControlEvents:UIControlEventTouchUpInside];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(img);
        make.centerY.equalTo(img);
        make.size.mas_equalTo(CGSizeMake(130, 130));
    }];
    btn = [[UIButton alloc]init];
    [img addSubview:btn];
    [btn addTarget:self action:@selector(useCode) forControlEvents:UIControlEventTouchUpInside];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(img);
        make.width.equalTo(@(140));
        make.bottom.equalTo(img).offset(-30);
    }];
    _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
    [self login];
    // Do any additional setup after loading the view.
}

- (void)onTimer {
    if (_canLogin) {
        [_timer invalidate];
        _timer = nil;
        id temp = [UIApplication sharedApplication].delegate;
        AppDelegate *delegate = temp;
        delegate.tab = [[SunTabController alloc]init];
        delegate.window.rootViewController = delegate.tab;
    }
}

- (void)useCode{
    SunLoginCheckVC *vc = [[SunLoginCheckVC alloc]init];
    vc.isLogin = YES;
    vc.phone = [[[NSUserDefaults standardUserDefaults]objectForKey:@"USER_INFO"]objectForKey:@"username"];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)login{
    LAContext* context = [[LAContext alloc] init];
    NSString* result = @"请按Home验证指纹";
    [context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:result reply:^(BOOL success, NSError *error) {
        if (error) {
            if (error.code == -8) {
                [[Sun shareSun]alertAlign:NSTextAlignmentLeft title:@"温馨提示" content:@"由于错误次数过多，指纹功能暂时被锁定。" left:@"知道了" right:nil cancel:^{
                    [[Sun shareSun]hideShow];
                } sure:nil To:self];
            }else {
                [[Sun shareSun]alertAlign:NSTextAlignmentLeft title:@"温馨提示" content:@"指纹验证失败，请使用密码登录。" left:@"知道了" right:nil cancel:^{
                    [[Sun shareSun]hideShow];
                } sure:nil To:self];
            }
        }else if (success) {
            _canLogin = YES;
            
//            [self dismissViewControllerAnimated:YES completion:nil];
//            [self.navigationController popToRootViewControllerAnimated:YES];
        }
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
